<template>
  <div class="familyHome">
    <a-card :loading="cardLoading" :bordered="false" style="height: 100%; margin-bottom: 20px">
      <family-header>
        <template slot="search">
          <a-input-search
            v-model="queryParam.familyName"
            style="width: 465px"
            enter-button="搜索"
            placeholder="输入族名称查询"
            @search="onSearch"
            allow-clear
          >
          </a-input-search>
        </template>
      </family-header>
    </a-card>
    <!-- 搜索 -->
    <a-card
      class="search j-address-list-right-card-box"
      :loading="false"
      :bordered="true"
      style="height: 100%; margin-bottom: 20px"
    >
      <div class="con">
        <div class="form-item">
          <div class="name"><span>品牌分类:</span></div>
          <div :class="['list', { isShowMore: advanced }]">
            <div class="item-body">
              <div
                :class="['item', { active: queryParam.brandId == item.brandId }]"
                v-for="(item, index) in getBrandListAll"
                :key="index"
                @click="changeBrand(item)"
              >
                {{ item.brandName }}
              </div>
            </div>
          </div>
          <a class="more" @click="toggleAdvanced" style="margin-left: 8px">
            {{ advanced ? '收起' : '展开' }}
            <a-icon :type="advanced ? 'up' : 'down'" />
          </a>
        </div>
      </div>
      <div class="con">
        <div class="form-item">
          <div class="name"><span>族库来源:</span></div>
          <div class="list">
            <div class="item-body">
              <div
                :class="['item', { active: queryParam.originId == item.value }]"
                v-for="item in originList"
                :key="item.value"
                :value="item.value"
                @click="changeOrigin(item)"
              >
                {{ item.text }}
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="con">
        <div class="form-item">
          <div class="name"><span>录入时间:</span></div>
          <div class="list">
            <a-range-picker v-model="queryParam.createTime" @change="changeTime"></a-range-picker>
          </div>
        </div>
      </div>
      <div class="con">
        <div class="form-item">
          <div class="name"><span>标签:</span></div>
          <div class="list">
            <a-tag v-for="tag in labelList" :key="tag.value" closable @close="() => handleCloseTag(tag)">
              {{ tag.name }}
            </a-tag>
          </div>
        </div>
      </div>
      <a-row type="flex" justify="start">
        <a-col>
          <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
            <a-col :md="3" :sm="24">
              <a-button @click="searchReset" type="primary" icon="reload" style="margin-left: 8px">重置</a-button>
            </a-col>
          </span>
          <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
            <a-col :md="3" :sm="24">
              <a-button style="margin-left: 8px" @click="searchQuery">确定</a-button>
            </a-col>
          </span>
        </a-col>
      </a-row>
    </a-card>
    <!-- 表格 -->
    <a-card>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        rowKey="id"
        :columns="columns"
        :dataSource="list"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleChangeInTable"
      >
        <template slot="price" slot-scope="text">
          <span style="color:#ff0000;font-weight: 600;">{{ text || '--' }}</span>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;"
          />
        </template>
        <template slot="quotationTime" slot-scope="text, record">
          <div style="position:relative;height:100px">
            <span
              style="display:inline-block;position:absolute;top:50%;transform: translate(-50%,-50%);font-size: 12px;"
              >{{ text || '--' }}</span
            >
            <div style="position:absolute;bottom:-5px;left:-10px;color:#1890FF">
              <span style="display:inline-block;padding-right:10px;">共1个来源,共1个品牌</span>
              <span @click="goDetail(record)" class="detail-link">
                查看详情
              </span>
            </div>
          </div>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script>
import { getAction } from '@/api/manage'
import { ajaxGetDictItems } from '@/api/api'

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FamilyHeader from './components/familyHeader.vue'
import _ from 'lodash'
export default {
  mixins: [JeecgListMixin],
  components: {
    FamilyHeader
  },
  data() {
    return {
      description: '族库分类',
      cardLoading: false,

      //
      formLayout: {
        labelCol: {
          span: 3
        },
        wrapperCol: {
          span: 15
        }
      },

      // 品牌
      brandList: [],
      selectBrandId: '',
      advanced: false, // 切换
      // 来源
      selectOriginId: '',
      originList: [],
      // 标签
      labelList: [],
      // 表格
      loading: false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '族ID',
          align: 'center',
          dataIndex: 'familyId'
        },
        {
          title: '族名称',
          align: 'center',
          dataIndex: 'familyName'
        },
        {
          title: '品牌',
          align: 'center',
          dataIndex: 'brandId_dictText'
        },
        {
          title: '来源',
          align: 'center',
          dataIndex: 'originId_dictText'
        },
        {
          title: '图片',
          align: 'center',
          dataIndex: 'familyImg',
          scopedSlots: { customRender: 'imgSlot' }
        },
        {
          title: '规格',
          align: 'center',
          dataIndex: 'familyStandard'
        },
        {
          title: '价格',
          align: 'center',
          dataIndex: 'price',
          scopedSlots: { customRender: 'price' }
        },
        {
          title: '报价时间',
          align: 'center',
          dataIndex: 'quotationTime',
          scopedSlots: { customRender: 'quotationTime' }
          // customRender: function(text) {
          //   return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          // }
        }
      ],
      // 表格数据
      list: [],
      // 分页参数
      // ipagination: {
      //   current: 1,
      //   pageSize: 5,
      //   pageSizeOptions: ['5', '10', '20'],
      //   showTotal: (total, range) => {
      //     return range[0] + '-' + range[1] + ' 共' + total + '条'
      //   },
      //   showQuickJumper: true,
      //   showSizeChanger: true,
      //   total: 0
      // },
      url: {
        list: '/familylibrary_list/familylibraryList_A/list',
        brandlist: '/familylibrary_brand/familylibraryBrand/list'
      }
    }
  },
  created() {
    this.init()
    this.getList()
  },
  methods: {
    init() {
      // 获取搜索-来源
      ajaxGetDictItems('fl_origin', null).then(res => {
        if (res.success) {
          console.log(res)
          this.originList = res.result
        }
      })
      // 获取搜索-品牌
      getAction(this.url.brandlist, { pageSize: 10000 }).then(res => {
        if (res.success) {
          this.brandList = res.result.records || res.result
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    // 获取列表
    getList() {
      let params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        ...this.queryParam,
        familyName: this.queryParam.familyName ? '*' + this.queryParam.familyName + '*' : null
      }
      getAction(this.url.list, { ...params }).then(res => {
        if (res.success) {
          this.list = res.result.records || res.result
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    // 重置
    searchReset() {
      this.queryParam = {}
      this.labelList = []
      this.getList()
    },
    // 确定
    searchQuery() {
      this.getList()
    },
    // 顶部名称搜索
    onSearch(value) {
      this.queryParam.familyName = value
      this.getList()
    },
    // 品牌展开收起
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    // 选择品牌
    changeBrand(item) {
      const { brandId: value, brandName: name } = item
      this.queryParam.brandId = item.brandId
      if (item.brandId == 'all') {
        return
      }
      this.labelList.push({ value, name, type: 1 })
      this.labelList = _.uniqBy(this.labelList, 'value')
    },
    // 选择来源
    changeOrigin(item) {
      const { value, text: name } = item
      this.queryParam.originId = value
      this.labelList.push({ value, name, type: 2 })
      this.labelList = _.uniqBy(this.labelList, 'value')
    },
    // 时间选择
    changeTime(value, dateString) {
      console.log(dateString[0], dateString[1])
      this.queryParam.createTime_begin = dateString[0]
      this.queryParam.createTime_end = dateString[1]
    },
    handleChangeInTable(pagination) {
     
      this.table.pagination = pagination
      this.loadData()
    },
    // 跳转至详情页
    goDetail(item) {
      console.log(item)
      this.$router.push({ path: '/familydetail?familyId=' + item.familyId })
    },
    // 标签关闭
    handleCloseTag(removedTag) {
      const tags = this.labelList.filter(tag => tag !== removedTag)
      this.labelList = tags
    }
  },
  computed: {
    getBrandListAll() {
      return [{ brandId: 'all', brandName: '全部' }, ...this.brandList]
    }
  }
}
</script>

<style lang="less" scoped>
.familyHome {
  display: flex;
  justify-content: center;
  flex-direction: column;
  padding: 0 50px;
  > div {
    width: 80%;
    margin: 0 auto;
  }
  .search {
    .con {
      display: flex;
      // justify-content: center;
      // align-items: center;
      .form-item {
        display: flex;
        .name {
          width: 100px;
          padding-top: 10px;
          display: flex;
        }
      }
      .list {
        padding-left: 15px;
        height: 40px;
        overflow: hidden;
        .item-body {
          height: 100%;
          display: flex;
          flex-wrap: wrap;
          .item {
            font-size: 12px;
            height: 40px;
            line-height: 40px;
            padding: 0 10px;
            cursor: pointer;

            &:hover {
              color: rgb(13, 149, 240);
            }
          }
          > .active {
            color: rgb(13, 149, 240);
          }
        }
      }
      .isShowMore {
        height: 100%;
      }
      .more {
        width: 100px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #fff;
        padding-left: 0px;
      }
    }
  }
  .detail-link {
    cursor: pointer;
    &:hover {
      color: rgb(1, 142, 250);
      font-weight: 700;
    }
  }
}
</style>
